package com.cn.esermis.dpld.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.esermis.dpld.entity.DataAcquisitionInstrument;
import com.cn.esermis.dpld.mapper.DataAcquisitionInstrumentMapper;
import com.cn.esermis.dpld.model.DataAcquisitionInstrumentVo;
import com.cn.esermis.dpld.model.Department;
import com.cn.esermis.dpld.model.OutletAndFactorVo;
import com.cn.esermis.dpld.service.IDataAcquisitionInstrumentService;
import com.cn.esermis.model.ComponyData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cn.esermis.dpld.model.AjaxResult.success;

@Service
public class DataAcquisitionInstrumentServiceImpl extends ServiceImpl<DataAcquisitionInstrumentMapper, DataAcquisitionInstrument> implements IDataAcquisitionInstrumentService {


    @Resource
    DataAcquisitionInstrumentMapper mapper;

    @Override
    public List<OutletAndFactorVo> selectlist() {
        return mapper.selectListAll();
    }

    @Override
    public List<ComponyData> getCompony() {
        List<ComponyData> list = mapper.getCompony();
        if (!list.isEmpty())
        {
            List<ComponyData> fu = list.stream().filter(d -> d.getLevelid() == null || d.getLevelid().length() == 4)
                    .collect(Collectors.toList());
            if (!fu.isEmpty()){
                for (ComponyData componyData : fu) {
                    if (componyData.getLevelid()!=null && componyData.getLevelid().length() == 4)
                    getComponyChild(componyData,list);
                }
            }
            return fu;
        }
        return new LinkedList<>();
    }

    public void getComponyChild(ComponyData fu,List<ComponyData> list) {
        if (fu.getLevelid() != null){
            String result = fu.getLevelid().substring(0, fu.getLevelid().length() - 2);
            if (!result.isEmpty()){
                List<ComponyData> child = list.stream().filter(d -> d.getLevelid()!=null
                        && d.getLevelid().length() == result.length()+4
                        &&!d.getLevelid().substring(0,result.length()+2).equals(fu.getLevelid())
                        && d.getLevelid().substring(0,result.length()+2).startsWith(result)).collect(Collectors.toList());
                if (!child.isEmpty()){
                    fu.setChildList(child);
                    for (ComponyData componyData : child) {
                        if (componyData.getLevelid()!=null){
                            getComponyChild(componyData,list);
                        }
                    }
                }
            }
        }
    }



    @Override
    public List<Map<String, Object>> getDepartment(String componyid) {
        return mapper.getDepartment(componyid);
    }

    @Override
    public Map<String, Object> getComponyMap() {
        return mapper.getComponyMap();
    }

    @Override
    public Map<String, Object> getDepartmentMap() {
        return mapper.getDepartmentMap();
    }

    @Override
    public List<DataAcquisitionInstrument> listByCode(DataAcquisitionInstrumentVo vo) {
        List<DataAcquisitionInstrument> dataAcquisitionInstruments = mapper.listByCode(vo);
        return dataAcquisitionInstruments;
    }

    @Override
    public List<String> getComponyList(String companyName, String companyId) {
        return mapper.getComponyList(companyName,companyId);
    }

    @Override
    public List<String> getComponyListAll(String companyName) {
        return mapper.getComponyListAll(companyName);
    }

    @Override
    public List<Department> getDepartmentDit() {
        return mapper.getDepartmentDit();
    }
}
